package com.user2.web;

import com.user2.config.PatternProperties;
import com.user2.pojo.User;
import com.user2.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user2")
@RefreshScope  //配置热更新
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PatternProperties properties;


//    @Value("${pattern.deteformat}")
//    private String deteformat;

    /**
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,
                          @RequestHeader(value = "Truth", required = false) String truth) {
        System.out.println("truth: " + truth);
        return userService.queryById(id);
    }

    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDeteformat()));
//        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(deteformat));
    }

    @GetMapping("prop")
    public PatternProperties porp(){
        return properties;
    }
}
